package com.mfhcd.facepay.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProviders;

import com.alibaba.android.arouter.launcher.ARouter;
import com.mfhcd.facepay.R;
import com.mfhcd.facepay.databinding.ActivityBaseBinding;
import com.mfhcd.facepay.model.PhotoBean;
import com.mfhcd.facepay.utils.ClassUtil;
import com.mfhcd.facepay.utils.DialogUtils;
import com.mfhcd.facepay.utils.ToastUtil;
import com.mfhcd.facepay.viewmodel.BaseViewModel;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

/**
 * Author：石强
 * E-Mail: shiqiang@mfhcd.com
 * Date：2019-09-05
 * 版权所有 © 现代金融控股（成都）有限公司
 */
public abstract class BaseActivity<VM extends BaseViewModel, SV extends ViewDataBinding> extends RxAppCompatActivity {
    // REQ CODE
    protected static final int REQUEST_PHOTO_CAPTURE = 998;
    protected static final int REQUEST_PHOTO_ALBUM = 999;
    protected static final int REQUEST_SCAN = 1000;
    // Constants
    protected static final int SMS_RESEND = 60;
    protected static final int SECONDS = 1000;
    // ViewModel
    protected VM viewModel;
    // 布局view
    protected SV bindingView;
    protected ActivityBaseBinding mBaseBinding;
    protected Context mContext;
    protected RxPermissions mRxPermissions;
    private boolean ignoreOrientation = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!ignoreOrientation)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        this.mContext = this;
        ARouter.getInstance().inject(this);
        mRxPermissions = new RxPermissions(this);
    }

    public void setIgnoreOrientation(boolean ignoreOrientation) {
        this.ignoreOrientation = ignoreOrientation;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void setContentView(int layoutResID) {
        mBaseBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_base, null, false);
        bindingView = DataBindingUtil.inflate(getLayoutInflater(), layoutResID, null, false);
        // content
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        bindingView.getRoot().setLayoutParams(params);
        FrameLayout mContainer = mBaseBinding.getRoot().findViewById(R.id.container);
        mContainer.addView(bindingView.getRoot());
        getWindow().setContentView(mBaseBinding.getRoot());
        //暂时注释掉导航栏的背景色
//        StatusBarUtil.setColor(this, getResources().getColor(R.color.color_status_bar));
        initViewModel();
        initTitleLayout();
        initData();
        initListener();
    }

    private void initTitleLayout() {
        mBaseBinding.ivTitleBack.setOnClickListener(v -> {
            finish();
        });
    }

    protected abstract void initData();

    protected abstract void initListener();

    public ActivityBaseBinding getBaseBinding() {
        return mBaseBinding;
    }

    public void hideTitle() {
        mBaseBinding.toolBar.setVisibility(View.GONE);
    }

    public void hideTitleRight() {
        mBaseBinding.ivTitleRight.setVisibility(View.GONE);
    }

    /**
     * 反转标题颜色：背景红色，图标文字白色
     */
    public void reverseTitle() {
        mBaseBinding.toolBar.setBackgroundColor(getResources().getColor(R.color.color_F9003E));
        mBaseBinding.ivTitleText.setTextColor(getResources().getColor(R.color.white));
        mBaseBinding.ivTitleBack.setImageResource(R.drawable.ic_back_white);
    }

    /**
     * 初始化ViewModel
     */
    private void initViewModel() {
        Class<VM> viewModelClass = ClassUtil.getViewModel(this);
        if (viewModelClass != null) {
            this.viewModel = ViewModelProviders.of(this).get(viewModelClass);
            this.viewModel.setCurrentActivity(this);
            this.viewModel.setBaseActivity(this);
        }
    }

    /**
     * 公共拍照/相册选择
     *
     * @param mPhotoBean
     */
    @SuppressLint("CheckResult")
    protected void showPicCaptureDialog(PhotoBean mPhotoBean) {
        mRxPermissions
                .request(
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
                .subscribe(granted -> {
                    if (granted) {
                        DialogUtils.showPicCaptureDialog(mPhotoBean, REQUEST_PHOTO_CAPTURE, REQUEST_PHOTO_ALBUM);
                    } else {
                        ToastUtil.showToast("请允许开启相机权限");
                    }
                });
    }

}